/*
 * Copyright	: (C) 2016 Shenzhen sgks micro semiconductor co., LTD
 * FileName		: osd.h
 * Description	: Internal OSD interface
 * Created		: wxz 2016-05-25
 * Version      : v2_005 
 * Modified		: 
 */

#ifndef _OSD_H_
#define _OSD_H_

#ifdef  __cplusplus
extern "C"
{
#endif

#define OSD_PLANE_NUM           (2)
#define OSD_AREA_NUM            (3)/*the number of areas of one plane.*/

#define OSD_FONT_PATH_MAX_LENGTH 256
#define OSD_TEXT_MAX_LENGTH 64
#define OSD_TRANSPARENT               (0)
#define OSD_NONTRANSPARENT            (255)

#define OSD_CLUT_COLOUR_NUM           (256)

#define OSD_DATE_THREAD_PRIORITY      (3)
#define OSD_DATE_THREAD_STACKSIZE     (1024)
#define OSD_DATE_THREAD_NAME          "osdDateThread"

#define	OSD_BITMAPFILE_HEADER_SIZE	  (14)
#define	OSD_BITMAPINFO_HEADER_SIZE	  (40)

#define	OSD_BMP_FILE_MAGIC_NO         (0x4D42)
#define	OSD_BMP_FILE_BIT_COUNT	      (8)

#define FT_INTEGER_BIT      (6)

#define SUCCESS 0
#define OSD_INIT_FAIL SUCCESS-50
#define OSD_FONTLIB_INIT_FAIL SUCCESS-51
#define OSD_CLOSE_FAIL SUCCESS-52
#define OSD_EXIT_FAIL SUCCESS-53
#define OSD_OPEN_FAIL SUCCESS-54
#define OSD_PROCESS_FAIL SUCCESS-55
#define OSD_PARAMERR SUCCESS-56

typedef struct font_attribute_s
{
	char type[OSD_FONT_PATH_MAX_LENGTH];
	unsigned int size;
    /*0: no outline.*/
	unsigned int outline_width;
    /*0: no bold at all; positive is bold, negetive is thin in horizontal.*/
	signed int   hori_bold;
    /*0: no bold at all; positive is bold, negetive is thin in vertical.*/
	signed int   vert_bold;
    /*0 is normal (no italic), 100 is 100% italic.*/
	unsigned int italic;
    /*0: anti-alias is enabled, 1: disable anti-alias. Default is 0.*/
	unsigned int disable_anti_alias;
} font_attribute_t;

typedef struct bitmap_info_s
{
	int width;
	int height;
} bitmap_info_t;

typedef struct pixel_type_s
{
	unsigned char pixel_background;
	unsigned char pixel_font;
	unsigned char pixel_outline;
	unsigned char reserved;
} pixel_type_t;


typedef enum {
    OSD_COLOUR_UNKNOW = -1,
    OSD_COLOUR_BLACK  = 0,
    OSD_COLOUR_RED,
    OSD_COLOUR_BLUE,
    OSD_COLOUR_GREEN,
    OSD_COLOUR_YELLOW,
    OSD_COLOUR_MAGENTA,
    OSD_COLOUR_CYAN,
    OSD_COLOUR_WHITE,
    OSD_COLOUR_NUMBERS,
}OSD_ColourEnumT;

typedef enum __osd_enc_font_type_e
{
    OSD_FONT_TYPE_FREETYPE = 0,
    OSD_FONT_TYPE_LATTICE,
} osd_enc_font_type_e;

typedef struct
{
    /*the enable/disable flag of area(0:disable, 1:enable).*/
    unsigned char       enable;
    /*font library file path.*/
    char                fontsPath[OSD_FONT_PATH_MAX_LENGTH];
    /*text string.*/
    char                textStr[OSD_TEXT_MAX_LENGTH];
    /*string length.*/
    unsigned int        textLen;
    /*font size.*/
    unsigned char       size;
    /*font color type.*/
    unsigned char       color;
    /*font outline width. 0: no outline*/
    unsigned int        outline;
    /*0: no bold at all; positive is bold, negetive is thin in horizontal.*/
    signed int          bold;
    /*0 is normal (no italic), 1 is 50% italic.*/
    unsigned int        italic;
    /*text area offset x, 0~100, 100 means 100% of encode width.*/
    unsigned short      startX;
    /*text area offset y, 0~100, 100 means 100% of encode height.*/
    unsigned short      startY;
    /*text area box width, 0~100, 100 means 100% of encode width.*/
    unsigned short      boxWidth;
    /*text area box height, 0~100, 100 means 100% of encode height.*/
    unsigned short      boxHeight;
    /*background color type.*/
    unsigned char       background;
    /*background enable. 0: no background.*/
    unsigned char       background_en;
}OSD_TextParamsT;


typedef struct
{
    /*the enable/disable flag of area(0:disable, 1:enable).*/
    unsigned char       enable;
    /*bmp file path&filename.*/
    char*               bmpFile;
    /*text area offset x.*/
    unsigned short      startX;
    /*text area offset y.*/
    unsigned short      startY;
}OSD_BmpParamsT;

#if 0
static pixel_type_t default_pixel_type =
{
    .pixel_background = 0,
    .pixel_outline = 1,
    .pixel_font = 255,
};
#else
static pixel_type_t default_pixel_type =
{
     0,
     1,
     255,
};
#endif
typedef struct span_s
{
    int x;
    int y;
    int width;
    int coverage;
} span_t;

typedef struct spans_s
{
    span_t span;
    struct spans_s* next;
} spans_t;

typedef struct rect_s
{
    int xmin;
    int xmax;
    int ymin;
    int ymax;
} rect_t;


typedef struct
{
    /*the enable/disable flag of show date info(0:disable, 1:enable).*/
    unsigned char       enable;
    /*the index of area.*/
    unsigned char       areaId;
    /*font size.*/
    unsigned char       fontSize;
    /*offset x.*/
    unsigned short      osdDateOffsetX[OSD_TEXT_MAX_LENGTH];
    /*offset y.*/
    unsigned short      osdDateOffsetY[OSD_TEXT_MAX_LENGTH];
}OSD_ShowDateParams;

typedef struct
{
    /*font pixel colour.*/
    OSD_ColourEnumT    font;
    /*outline pixel colour.*/
    OSD_ColourEnumT    outline;
}OSD_TextColorT;

typedef struct
{
    /*font data pointer.*/
    unsigned char               *data;
    /*font data length.*/
    unsigned int                dataLen;
    /*font box width.*/
    unsigned int                width;
    /*font box height.*/
    unsigned int                height;
}OSD_TextDataT;

/*colour look-up table struct of YUV domain.*/
typedef struct
{
    /*v value of yuv.*/
    unsigned char   v;
    /*u value of yuv.*/
    unsigned char   u;
    /*y value of yuv.*/
    unsigned char   y;
    /*transparent value.*/
    unsigned char   a;
}OSD_YUVClutT;


/*14 byptes*/
typedef struct
{
    /*bmp file header: file type.*/
	unsigned short	bfType;
    /*bmp file header: file size.*/
	unsigned int	bfSize;
    /*bmp file header: reserved.*/
	unsigned short	bfReserved1;
    /*bmp file header: reserved.*/
	unsigned short	bfReserved2;
    /*bmp file header: offset bits.*/
	unsigned int	bfOffBits;
}OSD_BmpHeader;

/*bmp inforamtion header 40bytes*/
typedef struct
{
    /*bmp inforamtion header: size*/
	unsigned int	biSize;
    /*bmp inforamtion header: width*/
	unsigned int	biWidth;
    /*bmp inforamtion header: height*/
    unsigned int	biHeight;
    /*bmp inforamtion header: planes*/
	unsigned short	biPlanes;
    /*bmp inforamtion header:  1,4,8,16,24 ,32 color attribute*/
	unsigned short	biBitCount;
	/*bmp inforamtion header: Compression*/
	unsigned int	biCompression;
    /*bmp inforamtion header: Image size*/
	unsigned int	biSizeImage;
	/*bmp inforamtion header: XPelsPerMerer*/
	unsigned int	biXPelsPerMerer;
    /*bmp inforamtion header: YPelsPerMerer*/
	unsigned int	biYPelsPerMerer;
    /*bmp inforamtion header: ClrUsed*/
	unsigned int	biClrUsed;
    /*bmp inforamtion header: ClrImportant*/
	unsigned int	biClrImportant;
}OSD_BmpInfoHeader;

/*clut, if 256 bit map it needs*/
typedef struct
{
	unsigned char	b;
	unsigned char	g;
	unsigned char	r;
	unsigned char	reserved;
} RGBQUAD;

#if 0
static OSD_YUVClutT osdColoTab[] =
{
    {   // black
        .y = 16,
        .u = 128,
        .v = 128,
        .a = OSD_NONTRANSPARENT,
    },
    {   // red
        .y = 82,
        .u = 90,
        .v = 240,
        .a = OSD_NONTRANSPARENT,
    },
    {   // blue
        .y = 41,
        .u = 240,
        .v = 110,
        .a = OSD_NONTRANSPARENT,
    },
    {   // green
        .y = 145,
        .u = 54,
        .v = 34,
        .a = OSD_NONTRANSPARENT,
    },
    {   // yellow
        .y = 210,
        .u = 16,
        .v = 146,
        .a = OSD_NONTRANSPARENT,
    },
    {   // magenta
        .y = 107,
        .u = 202,
        .v = 222,
        .a = OSD_NONTRANSPARENT,
    },
    {   // cyan
        .y = 170,
        .u = 166,
        .v = 16,
        .a = OSD_NONTRANSPARENT,
    },
    {   // white
        .y = 235,
        .u = 128,
        .v = 128,
        .a = OSD_NONTRANSPARENT,
    }
};
#else
static OSD_YUVClutT osdColoTab[] =
{
    {   // black
        16,
        128,
        128,
        OSD_NONTRANSPARENT,
    },
    {   // red
        82,
        90,
        240,
        OSD_NONTRANSPARENT,
    },
    {   // blue
        41,
        240,
        110,
        OSD_NONTRANSPARENT,
    },
    {   // green
        145,
        54,
        34,
        OSD_NONTRANSPARENT,
    },
    {   // yellow
        210,
        16,
        146,
        OSD_NONTRANSPARENT,
    },
    {   // magenta
        107,
        202,
        222,
        OSD_NONTRANSPARENT,
    },
    {   // cyan
        170,
        166,
        16,
        OSD_NONTRANSPARENT,
    },
    {   // white
        235,
        128,
        128,
        OSD_NONTRANSPARENT,
    }
};
#endif
static OSD_TextColorT  textColors[OSD_COLOUR_NUMBERS] =
{
    {OSD_COLOUR_BLACK,     OSD_COLOUR_WHITE},
    {OSD_COLOUR_RED,       OSD_COLOUR_BLACK},
    {OSD_COLOUR_BLUE,      OSD_COLOUR_WHITE},
    {OSD_COLOUR_GREEN,     OSD_COLOUR_BLACK},
    {OSD_COLOUR_YELLOW,    OSD_COLOUR_BLACK},
    {OSD_COLOUR_MAGENTA,   OSD_COLOUR_BLACK},
    {OSD_COLOUR_CYAN,      OSD_COLOUR_BLACK},
    {OSD_COLOUR_WHITE,     OSD_COLOUR_BLACK},
};


/*text insert library initialize parameters.*/
#if 0
static pixel_type_t pixelType =
{
    .pixel_background = 30,
    .pixel_outline    = 1,
    .pixel_font       = 255,
};
#else
static pixel_type_t pixelType =
{
    30,
    1,
    255,
};
#endif
void sys_get_date(char *str);
int osd_init();
int osd_exit();
int osd_open(int osd_font_type);
int osd_close();
int osd_exit();
int osd_function_show_text(unsigned short encodeWidth, unsigned short encodeHeight, int planeId, int areaId, OSD_TextParamsT *text);
int osd_function_show_date(unsigned short encodeWidth, unsigned short encodeHeight, int planeId,int areaId,OSD_TextParamsT *text);
int osd_lattice_initFont(void);
int osd_lattice_start(unsigned short encodeWidth,unsigned short encodeHeight, int planeId,int areaId,OSD_TextParamsT *text);
#ifdef  __cplusplus
}
#endif

#endif
